<template>
  <el-card class="good-container">
    <template #header>
      <div class="header">
        <el-button type="primary" :icon="Plus" @click="handleAdd">新增商品</el-button>
      </div>
    </template>
    <el-table :load="state.loading" :data="state.tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
      tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="goodsId" label="商品编号">
      </el-table-column>
      <el-table-column prop="goodsName" label="商品名">
      </el-table-column>
      <el-table-column prop="goodsIntro" label="商品简介">
      </el-table-column>
      <el-table-column label="商品图片" width="150px">
        <template #default="scope">
          <img style="width: 100px; height: 100px;" :key="scope.row.goodsId" :src="scope.row.goodsCoverImg" alt="商品主图">
        </template>
      </el-table-column>
      <el-table-column prop="stockNum" label="商品库存">
      </el-table-column>
      <el-table-column prop="sellingPrice" label="商品售价">
      </el-table-column>
      <el-table-column label="上架状态">
        <template #default="scope">
          <span style="color: green;" v-if="scope.row.goodsSellStatus == 0">销售中</span>
          <span style="color: red;" v-else>已下架</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="100">
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.goodsId)">修改</a>
          <a style="cursor: pointer; margin-right: 10px" v-if="scope.row.goodsSellStatus == 0"
            @click="handleStatus(scope.row.goodsId, 1)">下架</a>
          <a style="cursor: pointer; margin-right: 10px" v-else @click="handleStatus(scope.row.goodsId, 0)">上架</a>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <div class="pages">
      <div class="page_wrap">
        <span class="page_span1">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            :page-sizes="[10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="state.tableData.length">
          </el-pagination>
        </span>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { onMounted, reactive, getCurrentInstance, ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { Plus, Delete } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const app = getCurrentInstance()
const { goTop } = app.appContext.config.globalProperties
const router = useRouter()
const currentPage = ref(1)
const total = ref(0)
const pageSize = ref(10)

const handleSizeChange = (val) => {
  console.log(`每页 ${val} 条`);
  currentPage.value = 1;
  pageSize.value = val;
}

const handleCurrentChange = (val) => {
  console.log(`当前页: ${val}`);
  currentPage.value = val;
}
const state = reactive({
  loading: false,
  tableData: [], // 数据列表

})
onMounted(() => {
  getGoodList()
})
// 获取列表
const getGoodList = () => {
  state.loading = true
  axios.post('http://localhost:8080/goods/findAll').then(res => {
    state.tableData = res.data
  })
}
const handleAdd = () => {
  router.push({ path: '/admin/add' })
}
const handleEdit = (id) => {
  console.log(id);
  router.push({ path: '/admin/add', query: { id } })
}
/* 上下架操作 */
const handleStatus = (id, status) => {
  axios.post(`http://localhost:8080/goods/updateStatus/` + status + "/" + id).then(() => {
    ElMessage.success('修改成功')
    getGoodList()
  })
}
</script>

<style scoped>
.good-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>
